package com.jy.appmarket.screenlock.service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Notification;
import android.content.Intent;
import android.os.Build;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.accessibility.AccessibilityEvent;
import android.widget.RemoteViews;

import com.jy.appmarket.screenlock.LockScreenManager;
import com.jy.appmarket.screenlock.utils.CheckMessage;
import com.jy.appmarket.screenlock.utils.LockScreenConfig;
import com.rad.net.protocol.android.utils.LogUtils;

/**
 * 类说明：
 * 
 * @author  jie.li
 * @date    2015-5-6
 * @version 1.0.0
 */

public class SystemNotificationService extends AccessibilityService {
	private static final String TAG = "SystemNotificationService";
    public static final String NOTIFICATION_SERVICE = "system_notification_service";
    public static final String NOTIFICATION_SERVICE_PKG = "system_notification_service_pkg";
    public static final String ACTION_ACTIVE_SERVICE_NOTIFICATIONS = "action_active_service_notification";

	@Override
	public void onAccessibilityEvent(AccessibilityEvent paramAccessibilityEvent) {
		if (Build.VERSION.SDK_INT < 18) {
			String pkgName = "";
			Notification localNotification = null;
			if (paramAccessibilityEvent.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
				Parcelable localParcelable = paramAccessibilityEvent.getParcelableData();
				pkgName = (String) paramAccessibilityEvent.getPackageName();
				if ((localParcelable instanceof Notification)) {
					localNotification = (Notification) localParcelable;
	                int flag = localNotification.flags;
	                if(LockScreenConfig.isMessageTip(this) && (flag != Notification.FLAG_ONGOING_EVENT || flag != Notification.FLAG_NO_CLEAR) 
	                		&& !TextUtils.isEmpty(localNotification.tickerText)){
						if(CheckMessage.checkMessageValid(this, pkgName, localNotification.tickerText.toString())){		
			                List<String> list = getNotificationString(localNotification);
							if (list != null && list.size() > 0 && LockScreenManager.getInstance(this).isLocked()) {
								Intent it = new Intent(ACTION_ACTIVE_SERVICE_NOTIFICATIONS);
								it.putExtra(NOTIFICATION_SERVICE, localNotification);
								sendBroadcast(it);
							}
						}
	                }
				}
			}
		}
	}

	@Override
	protected void onServiceConnected() {
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
        info.notificationTimeout = 100;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
        setServiceInfo(info);
	}
	private List<String> getNotificationString(Notification paramNotification) {
		ArrayList<String> localArrayList = new ArrayList<String>();
		try {
			RemoteViews localRemoteViews = null;//paramNotification.bigContentView;
			if (localRemoteViews == null)
				localRemoteViews = paramNotification.contentView;
			Field localField1 = localRemoteViews.getClass().getDeclaredField(
					"mActions");
			localField1.setAccessible(true);
			Object localObject1 = localField1.get(localRemoteViews);
			if ((localObject1 instanceof List)) {
				@SuppressWarnings("rawtypes")
				Iterator localIterator = ((List) localObject1).iterator();
				while (localIterator.hasNext()) {
					Object localObject2 = localIterator.next();
					if (localObject2
							.getClass()
							.getName()
							.equals("android.widget.RemoteViews$ReflectionAction")) {
						Field localField2 = localObject2.getClass()
								.getDeclaredField("methodName");
						localField2.setAccessible(true);
						Object localObject3 = localField2.get(localObject2);
						if (((localObject3 instanceof String))
								&& ("setText".equals(localObject3))) {
							Field localField3 = localObject2.getClass()
									.getDeclaredField("value");
							localField3.setAccessible(true);
							Object localObject4 = localField3.get(localObject2);
							LogUtils.i(TAG,"value:" + localObject4);
							localArrayList.add(localObject4.toString());
						}
					}
				}
			}
		} catch (Exception localException) {
			localException.printStackTrace();
		}
		return localArrayList;
	}

	@Override
	public void onInterrupt() {
	}

}
